import React, { Component, Fragment } from 'react';
import { Link } from 'dva/router';
import { connect } from 'dva';
import TagRule from './tagRule'
import router from 'umi/router';
import styles from './index.less';
import { Card, Table, Divider, Radio, Button, Row, Col, Form, Icon, Tree, Spin, message } from 'antd';
import Input from '@/components/Input/index'
const RadioGroup = Radio.Group;
const FormItem = Form.Item;
const namespace = 'customertag'
@Form.create()
class AddTags extends React.Component {

  componentWillMount() {
    window.Isoption = false;
  }
  componentDidMount() {
    window.Isoption = true;
  }

    handleSubmit = (e) => {
        let { dispatch } = this.props;

        this.props.form.validateFields((err, values) => {
            if (!err) {
                dispatch({
                    type: `${namespace}/AddTag`,
                    param: values,
                    callback: (data) => {
                        if (data.Code == 0) {
                            message.success('添加成功')
                            router.replace({
                                pathname: `/${window.storeId}/customer/tags`,
                            });
                        } else {
                            message.error('添加失败')
                        }
                    }
                });

            }
        });
    }
    render() {
        const formItemLayout = {
            labelCol: {
                xs: { span: 6 },
                sm: { span: 6 },
                md: { span: 6 },

            },
            wrapperCol: {
                xs: { span: 8 },
                sm: { span: 8 },
                md: { span: 8 },
            },
        };
        const { loadBtn } = this.props;
        const {
            getFieldDecorator, getFieldsError, getFieldError, isFieldTouched, getFieldValue
        } = this.props.form;
        return (
            <Card bordered={false} className={styles.AddTags}>
                <Fragment>
                <Form >
                    <Row>
                        <Col>
                        <FormItem
                            {...formItemLayout}
                            //validateStatus={roleNameError ? 'error' : ''}
                            label="标签名称:"
                        >
                            {getFieldDecorator('Name', {
                                rules: [{
                                    required: true,
                                    message:'标签名称必须填写',

                                }],
                            })(
                                <Input  maxLength={6} showtotal />
                            )}
                        </FormItem>
                    </Col>
                    </Row>

                    <FormItem
                        {...formItemLayout}
                        label="标签类型"
                    >

                        {getFieldDecorator('tagType', {
                             initialValue:1,
                            rules: [{ required: true, message: '请选择标签类型!' }],
                        })(
                            <RadioGroup  >
                                <Radio value={1}>手动标签</Radio>
                                <Radio value={2}>自动标签</Radio>
                            </RadioGroup>
                        )}

                    </FormItem>
                    {getFieldValue("tagType") != 2 ? null :
                        <FormItem
                            // {...formItemLayout}
                            label=""
                            className={styles.TagRule}
                        >
                            {getFieldDecorator('TagRule', {
                                rules: [{ required: true, message: '请设置标签条件!' }],
                            })(
                                <TagRule form={this.props.form} />
                            )}

                        </FormItem>
                    }
                    <Row>
                        <Col offset={6}>
                            <Button
                                type="primary"
                                //htmlType="submit"
                                loading={loadBtn}
                                onClick={this.handleSubmit}
                            >
                                保存
                            </Button>
                        </Col>
                    </Row>
                </Form>
                </Fragment>
            </Card>
        )
    }
}
export default connect(({ customertag, global, loading }) => ({
    customertag,
    loadBtn: loading.effects['customertag/AddTag'],
}))(AddTags);
